//
//  SalaryQuaryVC.m
//  FindJob
//
//  Created by 233 on 16/2/29.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "SalaryQuaryVC.h"
#import "SalaryModel.h"
#import "InputTableViewCell.h"
#import "SelectedTableViewCell.h"
#import "SalaryQuaryResultVC.h"

#import "InfoSelectView.h"


@interface SalaryQuaryVC ()<UITableViewDataSource,UITableViewDelegate,InfoSelectViewDataSource,InfoSelectViewDelegate>
{
    NSInteger _selectedRow;
    NSInteger _selectedIndex;
    NSInteger _index;
}
@property (nonatomic, copy) NSArray *datas;
@property(nonatomic,strong)InfoSelectView *selectView;
@property(nonatomic,copy)NSArray *infoDatas;

//
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SalaryQuaryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.bounces = NO;
    [self.tableView reloadData];
    UINib *selectedNib = [UINib nibWithNibName:NSStringFromClass([SelectedTableViewCell class]) bundle:nil];
    [self.tableView registerNib:selectedNib forCellReuseIdentifier:kSelectedCellIdentifier];
    
    UINib *inputNib = [UINib nibWithNibName:NSStringFromClass([InputTableViewCell class]) bundle:nil];
    [self.tableView registerNib:inputNib forCellReuseIdentifier:kInputTableViewCellIdentifier];
}

- (IBAction)respondsToQuaryBtn:(UIButton *)sender {
}

#pragma mark -- UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.datas[indexPath.row];
    NSString *key = [dict allKeys][0];
    NSArray *array = dict[key];
    if (array.count == 0) {
        InputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kInputTableViewCellIdentifier];
        cell.titleLabel.text = key;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    SelectedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSelectedCellIdentifier];
    cell.titleLabel.text = key;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.subTitle.text = @"点击选择";
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.datas[indexPath.row];
    NSString *key = [dict allKeys][0];
    NSArray *array = dict[key];
    if (array.count == 0) {
        InputTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [cell.inputTextField becomeFirstResponder];
        
    }else{
        self.infoDatas = array;
        if ([self.selectView superview] == nil) {
            [self.view addSubview:self.selectView];
        }
//        [self.view addSubview:self.selectView];
        [self.selectView reloadAllComponents];
        if (self.selectView.numberOfCompents == 0) {
            // 设置默认选择的第一个分区的第0行
            [self infoSelectView:self.selectView didSelectRow:0 inComponent:0];
        }else{
            [self infoSelectView:self.selectView didSelectRow:0 inComponent:1];
        }
    }
    
    
    
}
#pragma mark -- 分区
- (NSInteger)numberOfCompentsInSelectView:(InfoSelectView *)infoSelectView{
    
    NSInteger compents = 1;
    for (id value in self.infoDatas) {
        if ([value isKindOfClass:[NSDictionary class]]) {
            compents = 2;
            break;
        }
    }
    return compents;
}
- (NSInteger)infoSelectView:(InfoSelectView *)infoSelectView numberOfRowsInCompent:(NSInteger)compent{
    if (infoSelectView.numberOfCompents == 2) {
        if (compent == 0) {
            return self.infoDatas.count;
        }
        NSInteger selectIndex =  [infoSelectView selectRowInComponet:0];
        
        NSDictionary *dict = self.infoDatas[selectIndex];
        NSArray *array = dict[@"Cities"];
        return array.count;
        
    }else{
        return self.infoDatas.count;
    }
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CGRectGetHeight(tableView.frame) / self.datas.count;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}
- (NSString *)infoSelectView:(InfoSelectView *)infoSelectView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (infoSelectView.numberOfCompents == 2) {
       
        if (component  == 0) {
            NSDictionary *dict = self.infoDatas[row];
            return dict[@"State"];
        }else{
            // 获取第一个分区选中的行
            NSInteger selectedIndex = [infoSelectView selectRowInComponet:0];
            // 获取选中的字典、选中的哪一行
            NSDictionary *dict = self.infoDatas[selectedIndex];
            NSArray *cities = dict[@"Cities"];
            NSDictionary *cityInfo = cities[row];
            return cityInfo[@"city"];
            
        }
    }else{
        // 只要一个分区是直接返回当前的值
        return self.infoDatas[row];
    }
    
}

- (void)infoSelectView:(InfoSelectView *)infoSelectView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    // 获取选中的cell
    SelectedTableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];
    
    if (infoSelectView.numberOfCompents == 2) {
        NSString *subTitle = @"";
        if (component == 0) {
            // 获取选中的字典、选中的哪一行
            NSDictionary *dict = self.infoDatas[row];
            NSArray *cities = dict[@"Cities"];
            NSDictionary *cityInfo = cities[0];
            subTitle = cityInfo[@"city"];
        }else{
            // 获取第一个分区选中的行
            NSInteger selectedIndex = [infoSelectView selectRowInComponet:0];
            // 获取选中的字典、选中的哪一行
            NSDictionary *dict = self.infoDatas[selectedIndex];
            NSArray *cities = dict[@"Cities"];
            NSDictionary *cityInfo = cities[row];
            subTitle = cityInfo[@"city"];
        }
        cell.subTitle.text = subTitle;
        
    }else{
        cell.subTitle.text = self.infoDatas[row];
    }
    
}
- (void)respondsToPressedSureBtn{
//    SelectedTableViewCell *cell = [self.tableView cellForRowAtIndexPath:_tableView.indexPathForSelectedRow];
//    InfoSelectView *infoSelectView = [[InfoSelectView alloc] init];
//    
//    if (infoSelectView.numberOfCompents == 2) {
//        
//        cell.subTitle.text = self.infoDatas[_selectedRow];
//    }
//    cell.subTitle.text = self.infoDatas[_index];
    [self.selectView removeFromSuperview];
}
#pragma mark -- navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if ([segue.destinationViewController isKindOfClass:[SalaryQuaryResultVC class]]) {
        SalaryQuaryResultVC *sarVc = segue.destinationViewController;
        sarVc.requstArr = [self gainSelectedInfoArray];
    }
}
#pragma mark -- private
/** 获取选中的值 */
- (NSArray *)gainSelectedInfoArray{
    NSMutableArray *reslutArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.datas.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        id cell = [self.tableView cellForRowAtIndexPath:indexPath];
        if ([cell isKindOfClass:[SelectedTableViewCell class]]) {
            NSString *value = [(SelectedTableViewCell *)cell subTitle].text;
            [reslutArr addObject:value];
        }else{
            NSString *value = [(InputTableViewCell *)cell inputTextField].text;
            [reslutArr addObject:value];
        }
    }
    return reslutArr;
}
#pragma mark -- getters
- (NSArray *)datas{
    if (!_datas) {
        
        SalaryModel *model = [[SalaryModel alloc] init];
        _datas = [model gainSalaryModels];
        
    }
    return _datas;
}
- (InfoSelectView *)selectView{
    if (!_selectView) {
        _selectView = [[InfoSelectView alloc] initWithDataSoure:self delegate:self];
    }
    return _selectView;
}
@end
